﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class ContractsCollection : AbstractCollection
    {
        public List<Contracts> ContractsList
        {
            get { return Entities.Contracts.ToList(); }
        }

        #region Overrides of AbstractCollection

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(ContractsList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (Contracts);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.Contracts.Find(itemId);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            throw new NotImplementedException();
        }

        #endregion

        public new bool Update(object item, string userLogin)
        {
            var newObject = (Contracts) item;
            if (newObject == null)
                return false;


            var object4Update = GetItemByIdFromDatabase(newObject.GetKeyValue()) as Contracts;
            if (object4Update == null)
                return false;

            if (newObject.IdAgreementElement > 0) object4Update.IdAgreementElement = newObject.IdAgreementElement;
            if (newObject.IdProfineEssentialElement > 0)
                object4Update.IdProfineEssentialElement = newObject.IdProfineEssentialElement;
            if (!string.IsNullOrEmpty(newObject.ContractNumber))
                object4Update.ContractNumber = newObject.ContractNumber;
            if (newObject.CreationDate > new DateTime(2010)) object4Update.CreationDate = newObject.CreationDate;
            if (newObject.ValidTill != null)
                if (newObject.ValidTill > new DateTime(2010)) object4Update.ValidTill = newObject.ValidTill;
            if (newObject.SigningDate > new DateTime(2010)) object4Update.SigningDate = newObject.SigningDate;
            if (newObject.CustomerSigningDate > new DateTime(2010))
                object4Update.CustomerSigningDate = newObject.CustomerSigningDate;
            if (newObject.IdContractType > 0) object4Update.IdContractType = newObject.IdContractType;
            if (newObject.IdContractStatus > 0) object4Update.IdContractStatus = newObject.IdContractStatus;
            if (newObject.Id1stCopyHolder > -1) object4Update.Id1stCopyHolder = newObject.Id1stCopyHolder;
            if (newObject.Id2ndCopyHolder > -1) object4Update.Id2ndCopyHolder = newObject.Id2ndCopyHolder;
            if (newObject.GettingSignedCopyDate > new DateTime(2010))
                object4Update.GettingSignedCopyDate = newObject.GettingSignedCopyDate;
            if (!string.IsNullOrEmpty(newObject.Comments)) object4Update.Comments = newObject.Comments;
            if (newObject.IdParentContract > 0) object4Update.IdParentContract = newObject.IdParentContract;


            return base.Update(item, userLogin);
        }
    }
}